﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Feedback
{
    public partial class View : BasePage
    {
        protected static int totalFeedback;
        protected int j = 1;

        public override void InitControlResource()
        {
            totalFeedback = FeedbackPresent.GetList(p => p.Status == (int)FeedbackStatus.Completed && p.IsPublished == true).Count;
            lblHeader1.Text = string.Format(NameResource.lblFeedbackHeader, totalFeedback);
            dlFeedbackQuestion.DataSource = QusetionPresent.GetList(q => q.Status == (int)QuestionStatus.Active);
            dlFeedbackQuestion.DataBind();
        }

        protected void dlFeedbackQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.Question question = e.Item.DataItem as Model.Question;

                Chart chartFeedBack = e.Item.FindControl("ChartFeedBack") as Chart;
                chartFeedBack.Visible = true;
                chartFeedBack.ImageType = ChartImageType.Png;

                //设置图表的数据源
                chartFeedBack.DataSource = GetChartData(question.QuestionId);

                chartFeedBack.Series[0]["PieDrawingStyle"] = "Concave";

                //设置图表Y轴对应项
                chartFeedBack.Series[0].YValueMembers = "AnswerCount";
                chartFeedBack.Series[0].Label = "#PERCENT";

                //设置图表X轴对应项
                chartFeedBack.Series[0].XValueMember = "Answer";

                chartFeedBack.Legends[0].Enabled = true;

                chartFeedBack.Legends[0].CellColumns[1].Text = "#AXISLABEL";
                chartFeedBack.Legends[0].CellColumns[2].Text = "#VALY";

                //绑定数据
                chartFeedBack.DataBind();
            }
        }
        class ChartData
        {
            public string Answer { set; get; }
            public int AnswerCount { set; get; }
        }

        private List<ChartData> GetChartData(int questionID)
        {
            var ret = new List<ChartData>();
            var answer = AnswerPresent.GetList(a => a.QuestionId == questionID);
            var feedBack = FeedbackPresent.GetList();
            var feedbackAnswer = FeedbackAnswerPresent.GetByQuestionId(questionID);

            if (feedbackAnswer != null)
            {
                var feedbackAnswerGroup = (from p in feedbackAnswer
                                           join f in feedBack on p.FeedbackId equals f.FeedbackId
                                           where f.Status == (int)FeedbackStatus.Completed && f.IsPublished == true
                                           group p by p.AnswerId into g
                                           select new { g.Key, count = g.Count() }).ToList();

                foreach (var a in answer)
                {
                    var fbaGrp = feedbackAnswerGroup.Find(q => q.Key.Value == a.Id);
                    var countAnswer = fbaGrp == null ? 0 : fbaGrp.count;
                    ret.Add(new ChartData
                    {
                        Answer = Language == Utility.Language.CN ? a.Answer_CN : a.Answer_EN,
                        AnswerCount = countAnswer
                    });
                }
            }
            return ret;
        }
    }
}